package com.dementors.ui.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.dementors.R;
import com.dementors.base.BaseFragment;
import com.dementors.ui.fragmentItem.FragmentSeaItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/1 0001.
 */
public class FragmentSea extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager vPager;
    private MyPagerAdapter mAdapter;
    private FragmentManager fm;
    private List<String> mTitleList = new ArrayList<>();//页卡标题集合
    private List<Fragment> mViewList = new ArrayList<>();//页卡视图集合


    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_sea;
    }

    @Override
    protected void initViews() {
        mTabLayout = (TabLayout) view.findViewById(R.id.tabs_sea);
        vPager = (ViewPager) view.findViewById(R.id.vp_view_sea);
    }

    @Override
    protected void initData() {
        initFragment();//初始化Fragment1中的每个小Fragment
        initTitle();//初始化标题
        initTablayout();//初始化tablayout
    }

    private void initTablayout() {
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < 6; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(i)));//添加tab选项卡
        }
        fm = getChildFragmentManager();
        mAdapter = new MyPagerAdapter(fm);
        vPager.setAdapter(mAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(vPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
    }

    private void initTitle() {
        mTitleList.add("上新");
        mTitleList.add("纸尿裤");
        mTitleList.add("奶粉");
        mTitleList.add("食品");
        mTitleList.add("用品");
        mTitleList.add("美妆个护");
    }

    private void initFragment() {
//        FragmentItemFirst i1 = new FragmentItemFirst();
//        Bundle bundle1 = new Bundle();
//        bundle1.putInt("type", 0);
//        i1.setArguments(bundle1);
//        mViewList.add(i1);
        for (int i = 0; i < 6; i++) {
            FragmentSeaItem frag = new FragmentSeaItem();
            Bundle bundle = new Bundle();
            bundle.putInt("type", i);
            frag.setArguments(bundle);
            mViewList.add(frag);

        }

    }

    //ViewPager适配器
    class MyPagerAdapter extends FragmentPagerAdapter {


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mViewList == null ? 0 : mViewList.size();//页卡数
        }

        @Override
        public Fragment getItem(int position) {
            return mViewList.get(position);
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);//页卡标题
        }

    }


}
